package com.luyuan.crm.sale.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.luyuan.crm.sale.model.Order;
import com.luyuan.crm.sale.model.OrderRecord;
import com.luyuan.crm.sale.service.OrderRecordService;
import com.ruijie.foundation.dao.support.HibernateEntityDao;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaQueryTemplate;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;

@SuppressWarnings("unchecked")
public class OrderRecordServiceImpl extends HibernateEntityDao<OrderRecord> implements OrderRecordService {

    @Override
    public Page pagedQuery(CriteriaTemplate criteria, int pageNo, int pageSize,
            List<org.hibernate.criterion.Order> orders) {
        CriteriaQueryTemplate cqt = new CriteriaQueryTemplate(getSessionFactory());
        return cqt.pagedQuery(criteria, pageNo, pageSize, orders);
    }

    @Override
    public List<OrderRecord> getRecordByOrder(Order order) {
        List<OrderRecord> orderRecords = new ArrayList<OrderRecord>();
        orderRecords = find("from OrderRecord where order.id=?", order.getId());
        return orderRecords;
    }

    @Override
    public void clearLog() {
        Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
        ca.setTime(new Date()); // 设置时间为当前时间
        ca.add(Calendar.YEAR, -2); // 年份减1
        Date lastMonth = ca.getTime(); // 结果
        logger.info("clean Order Record log before: " + lastMonth);
        update("delete OrderRecord where operateTime < ?", lastMonth);
    }
}
